function login(){
    var username=$("#username").val();
    var password=$("#password").val();
    if(username==null||username==undefined||username==""){
        showDialog('Warn','username is not null!');
        return;
    }
    if(password==null||password==undefined||password==""){
        showDialog('Warn','password is not null!');
        return;
    }
    $.cookie('username', username);
    getData('POST',interfaces.login,true,'{"username":"'+username+'","password":"'+password+'"}','login');
}
$("#username").val($.cookie('username'));
seajs.use(['jquery', 'lib/dialog','lib/jquery.cookie','common/common'], function($, dialog,cookie,common) {
    if(dev) {
        console.info('加载', 'login/init.js');
    }
    $(document).ready(function() {
        $.validator.setDefaults({
            ignore: []
        });
        $("#reg").validate({
            errorPlacement: function(error, element) {
                error.appendTo(element.parent());
            }
        });
        $("#forget").validate({
            errorPlacement: function(error, element) {
                error.appendTo(element.parent());
            }
        });
    	$('#interests').bind('input propertychange', function() {
            if(dev) {
                console.info($("#interests").val());
            }
    		$("#interests").val($("#interests").val().replace(/，/ig,',')); 
		});
		$('body').delegate('.showSignup','click',function(){
            $(':input','#reg').val('');
            $("label").remove(".error");
            $("ul.tag-editor>li").not(0).remove();
            $("ul.tag-editor").append('<li class="placeholder"><div>Please enter your interest, and more interest in the Enter key to increase</div></li>');
            $('#regdiv').show();
		});
		$('body').delegate('#signin','click', function () {
            login();
        });
        $('#username,#password').bind('keypress',function(event){
            if(event.keyCode == "13"){
                login();
            }
        });

		$('body').delegate('#regclose','click',function(){
			$('#regdiv').hide();
		});
        $('body').delegate('#forgetclose','click',function(){
            $('#forgetpassworddiv').hide();
        });
		$('body').delegate('.visitor','click',function(){
            $('#forgetpassworddiv').show();
		});

		$('body').delegate('#signupSignup','click',function(){
            if(dev) {
                console.info("注册");
            }
            if($("#reg").validate().form()){
                var data = $("#reg").serializeJson(); //自动将form表单封装成json
                if(dev) {
                    console.info(data);
                }
                var interests=data.interests;
                if(interests!=null&&interests!=""){
                    var arr=interests.split(',');
                    if(dev) {
                        console.info(arr);
                    }
                    data['interests']=arr;
                }else{
                    data['interests']=new Array();
                }
                if(dev) {
                    console.info(interests);
                    console.info(JSON.stringify(data));
                }
                getData('POST',interfaces.register,false,JSON.stringify(data),'register');
            }
		});


        $('body').delegate('#find','click',function(){
            if(dev) {
                console.info("找回密码");
            }
            if($("#forget").validate().form()){
                var data = $("#forget").serializeJson();
                getData('POST',interfaces.findpassword,false,JSON.stringify(data),'findpassword');
            }
        });



    });
});


seajs.use('css/jquery.tag-editor.css');
seajs.use(['jquery','lib/jquery.tag-editor.min.js'],function($,tagsinput){
    $(function () {
        //$('#interests').tagsInput();
        //$('#interests').tagEditor({ initialTags: ['Hello', 'World', 'Example', 'Tags'], delimiter: ', ', placeholder: 'Enter tags ...' }).css('display', 'block').attr('readonly', true);
        $('#interests').tagEditor({

            initialTags: [],
            delimiter: ',',
            placeholder: 'If inputting multi-interest, use ‘Enter’ key please.',
            onChange: function(field, editor, tags) { $('#interests').text((tags.length ? tags.join(',') : '')); }

        }).attr('readonly', true);
    });
});
